<?php

namespace App\Http\Controllers\Admin;

use App\Models\Auth;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class AuthController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 用datatable  直接把所有数据传过去即可
        $data = (new Auth)->getAllList();  
        return view('admin.auth.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data = Auth::where('pid',0)->get();
        return view('admin.auth.create', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 异常处理
        try{
            $this->validate($request,[
                'name' => [
                    'required',
                    //验证字段唯一性（除软删除数据以外）
                    Rule::unique('auths')->where(function($query){
                        $query->where('deleted_at',null);
                    })
                ],
            ]);
        }catch (\Exception $e){
            return ['status'=>1000,'msg'=>'验证不通过'];
        }
        Auth::create($request->except('_token'));
        return ['status'=>0,'msg'=>'添加权限成功'];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Auth  $auth
     * @return \Illuminate\Http\Response
     */
    public function show(Auth $auth)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(int $id)
    {
        //
        $model = Auth::find($id);
        $data = Auth::getTopAuth();
        // dd($data);
        return view('admin.auth.edit', compact('model','data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, int $id)
    {
        //异常处理
        try {
            $this->validate($request,[
                // 'info.name'=>'required|unique:auths,name,' . $id . ',id'
                'info.name' => [
                    'required',
                    //验证字段唯一性（除软删除数据以外）
                    Rule::unique('auths','name')->where(function($query){
                        $query->where('deleted_at',null)->where('id','<>', request('info.id'));
                    })
                ],
                'info.created_at' => [
                    'required',
                    'date_format:"Y-m-d H:i:s"'
                ]
            ]);
        } catch (\Exception $e){
            return ['status'=>1000, 'msg'=>'验证不通过!'];
        }
        // 修改角色入库
        Auth::where('id',$id)->update($request->info);
        return ['status'=> 0, 'msg'=> '修改权限成功'];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(int $id)
    {
        //需要注意，如果删除顶级权限，其子权限也要全部删除
        $model = Auth::find($id);
        $arrIds = [];
        if($model->pid == 0) {
            $arrIds = Auth::where('pid',$id)->select('id')->get()->toArray();

        }
        if($arrIds){
            $arrIds = array_column($arrIds,'id');
            //删除子权限
            Auth::whereIn('id',$arrIds)->delete();
        }
        // dd($arrIds);
        $model->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
}
